﻿var g_user = '';
jQuery.User = {
    user: "lol"
};

function login_joinButtons() {
    $('div.drop-down-link').hide();

    $('div.drop-down-link').click(function (e) {

        if (e.target.type != 'submit') {
            $(this).toggleClass('active').children('div.drop-down-form').toggle();

            var inputs = $(this).children('div.drop-down-form').children().children();
            inputs.first().focus();
        }
    });

    
    $('div.drop-down-form').click(function (e) {
        if (e.target.type != 'submit')
           return false;
    });

    $(document).click(function (e) {
        if (e.target.tagName === 'SPAN') {
            if (e.target.innerHTML === 'Login')
                $('div.drop-down-link#join').removeClass('active').children('div.drop-down-form').hide();
            else if (e.target.innerHTML === 'Join') {
                $('div.drop-down-link#login').removeClass('active').children('div.drop-down-form').hide();
            }
            return false;
        }

        if (e.target.type != 'submit') {
            $('div.drop-down-link').removeClass('active').children('div.drop-down-form').hide();
        }
    });

    $('div.drop-down-link#user-name').click(function () {
        $('#header-tabs .nav li a[href=#Account]').click();
        $(this).removeClass('active');
    });

    $('div.drop-down-link#logout').click(function () {
        $.ajax(
        {
            url: 'api/login/logout.php',
            success: function (data) {
                console.log(data);

                userLoggedOut();
                g_user = '';
            },
            error: function (data) {
                console.log(data);
                alert('Error logging out.\nPlease try again.');
            }
        });

        return false;
    });

    $('div.drop-down-form#login-form').submit(function (e) {
        e.preventDefault();

        var _user = $('div.drop-down-form#login-form input[name=user]').val();
        var _pass = $('div.drop-down-form#login-form input[name=pass]').val();

        $.ajax({
            type: 'POST',
            url: "api/login/login.php",
            dataType: 'json',
            data: {
                user: _user,
                pass: _pass,
                xml: '0'
            },
            success: function (data) {
                $.each(data.login.code, function (i, val) {
                    loginErrorHandlers[val](data);
                });
                console.log(data.login.code);
                console.log(data.login.message);

                setHeaderBar();
            },
            error: function (data) {
                ajaxError();
            }
        });

    });

    $('div.drop-down-form#join-form').submit(function (e) {
        e.preventDefault();

        var $user = $('div.drop-down-form#join-form [name=user]').val();
        var $pass = $('div.drop-down-form#join-form [name=pass]').val();
        var $pass2 = $('div.drop-down-form#join-form [name=pass2]').val();
        var $email = $('div.drop-down-form#join-form [name=email]').val();
        var $birth = $('div.drop-down-form#join-form [name=birth]').val();

        if ($pass != $pass2) {
            joinError1();
        }

        // Verificar birth

        // for some idiotic reason, javascript doesn't suport md5 hashing.
        // load crypto.js plugin
        
        $.ajax({
            type: 'POST',
            url: "api/login/register.php",
            dataType: 'json',
            data: {
                user:  $user,
                pass:  $pass,
                pass2: $pass2,
                email: $email,
                birth: $birth,
                xml: '0'
            },
            success: function (data) {

                $.each(data.registration.code, function (i, val) {
                    joinErrorHandlers[val](data);
                });
            },
            error: function (data) {
                ajaxError();
            }
        });
    });

    setHeaderBar();
}

function setHeaderBar() {
    $.ajax({
        type: 'GET',
        url: 'api/login/isLogged.php',
        dataType: 'json',
        data: { xml: '0' },
        success: function (data) {
            if (data['user']) {
                g_user = data['user'];
                $.User.user = g_user;
                get_links('featured', 'Account', '', g_user, '0', '0');
                addLink();
            }
            else {
                $('ul#Account').append('<li> Login to see your personal links </li>');
            }

            showDropDownMenu();
        },
        error: function () { ajaxError() }
    });
}

function showDropDownMenu() {
    if (g_user != '') {
        $('div.drop-down-link').not('#logout').not('#user-name').hide();
        $('div.drop-down-link#logout').show();
        $('div.drop-down-link#user-name').show().children('span').text(g_user);
    }
    else {
        $('div.drop-down-link').not('#logout').not('#user-name').show();
        $('div.drop-down-link#logout').hide();
        $('div.drop-down-link#user-name').hide();
    }
}

function userLoggedIn() {
    $('#user-name').children('span').text(g_user);

    $('div.drop-down-link ').not('#logout').not('#user-name').fadeOut('slow', function () {
        $('div.drop-down-link#logout').fadeIn('slow').removeClass('active');
        $('div.drop-down-link#user-name').fadeIn('slow');
    });
}


function userLoggedOut() {
    $('ul#Account li').empty();
    $('ul#Account').append('<li> Login to see your personal links </li>');
    $('div.drop-down-link#user-name').fadeOut('slow');
    $('div.drop-down-link#logout').fadeOut('slow', function () {
        $('div.drop-down-link').not('#logout').not('#user-name').fadeIn('slow');
    });
}


function isLogged() {
    var $return;
    $.ajax({
        type: 'GET',
        url: 'api/login/isLogged.php',
        dataType: 'json',
        data: { xml: '0' },
        success: function (data) {
            console.log('isLogged: ' + data.user);
            if (jQuery.isEmptyObject(data.error)) {
                alert("isLogged");
                g_user = data.user;
                window.user = data.user;
            }
            else {
                g_user = '';
            }
        },
        error: function () { g_user = ''; }
    });
    return $return;
}

function addLink() {

    $('#addlinkform').submit(function (e) {

        e.preventDefault();

        var $tagx = $('input#a3').attr('value');
        var $tags = $tagx.split(' ');
        $.ajax({
            type: 'GET',
            url: 'api/link/addLink.php',
            dataType: 'json',
            data: {
                tags: $tags,
                link: $('input#a0').attr('value'),
                date: $('input#a2').attr('value'),
                desc: $('input#a1').attr('value'),
                xml: '0'
            },
            success: function (data) {
                console.log('link added');

            },
            error: function (data) {
                console.log('error link adding');
            }
        });

        $('ul#Account li').remove();
        $('ul#Account div.index').remove();
        $('ul#Account div.tag').remove();
        $('ul#Account a').remove();
        $('ul#Account br').remove();
        //$('ul#Account a').empty();
        get_links('featured', 'Account', '', g_user, '0', '0');
    });
}